package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartBizService;
import com.atguigu.gmall.cart.service.impl.CartBizServiceImpl;
import com.atguigu.gmall.cart.vo.AddCartSuccessVo;
import com.atguigu.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/inner/cart")
@RestController
@Slf4j
public class CartRpcController {

    @Autowired
    CartBizService cartBizService;


    @GetMapping("/add/{skuId}")
    public Result<AddCartSuccessVo> addToCart(@PathVariable Long skuId
            , @RequestParam("skuNum") Integer skuNum
    ) {
        String cartKey = CartBizServiceImpl.determinCartKey();
        AddCartSuccessVo addCartSuccessVo = cartBizService.addToCart(skuId, skuNum, cartKey);
        log.info("添加：商品{}到购物车", skuId);
        return Result.ok(addCartSuccessVo);
    }

    /**
     * 删除全部选中的
     * @return
     */
    @DeleteMapping("/deleteChecked")
    public Result deleteChecked(){
        String cartKey = CartBizServiceImpl.determinCartKey();
        cartBizService.deleteChecked(cartKey);
        return Result.ok();
    }

    /**
     * 获取选中的所有东西
     * @return
     */
    @GetMapping("/checkeds")
    public Result<List<CartInfo>> getChecked(){
        String cartKey = CartBizServiceImpl.determinCartKey();
        List<CartInfo> checkeds = cartBizService.getCheckeds(cartKey);
        return Result.ok(checkeds);
    }
}
